class_name PersonCenter extends Node2D

var is_can_startGame = false    # 登录后等会儿再点开始游戏，websocket还没发过去数据
var is_matching = false  # 正在匹配中
var second = 0           # 秒，匹配时间

func _ready():
	ObjectManager.is_inGame = false
	ObjectManager.personCenter = self
	$"个人信息/昵称".text = ObjectManager.myUserInfo.username
	$"个人信息/战力".text = "战斗力：" + String.num_int64(ObjectManager.myUserInfo.score)
	$"匹配中".visible = false
	# 启动webSocket,延时发送绑定数据
	if !WebSocketManager.is_socketConnected:
		WebSocketManager.init_to_connect()
		await get_tree().create_timer(.5).timeout
		WebSocketManager.sendObj(false,"userBind",{"username":ObjectManager.myUserInfo.username})
	is_can_startGame = true
	


func _on_匹配对战_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	if is_matching:
		ObjectManager.toast.showToast("已经在匹配了")
		return
	if !is_can_startGame:
		await get_tree().create_timer(0.5).timeout
	is_matching = true
	$"匹配中".visible = true
	$"匹配中/Timer".start()
	WebSocketManager.sendObj(false,"match",{"username":ObjectManager.myUserInfo.username})

func _on_观战_pressed() -> void:
	ObjectManager.soundManager.play(SoundType.button_click)
	if is_matching:
		ObjectManager.toast.showToast("匹配中，不能观战")
		return
	WebSocketManager.sendObj(false,"watchingRoom",{})

func _on_复盘_pressed() -> void:
	ObjectManager.soundManager.play(SoundType.button_click)
	if is_matching:
		ObjectManager.toast.showToast("匹配中，不能复盘")
		return
	$"棋谱下载网络请求".downloadManual()

func _on_约定对战_pressed():
	ObjectManager.toast.showToast("尚未开放")
	

func _on_取消匹配_pressed():
	ObjectManager.soundManager.play(SoundType.button_click)
	is_matching = false
	$"匹配中".visible = false
	$"匹配中/Timer".stop()
	second = 0
	_on_timer_timeout()
	WebSocketManager.sendObj(false,"cancelMatch",{"username":ObjectManager.myUserInfo.username})

func _on_timer_timeout():
	var minute = int(float(second) / 60)
	if minute<10:
		minute = "0"+String.num_int64(minute)
	else :
		minute = String.num_int64(minute)
	var remain = second%60
	if remain<10:
		remain = "0"+String.num_int64(remain)
	else :
		remain = String.num_int64(remain)
	$"匹配中/Label".text = minute + ":" + remain
	second += 1
